Xbasic

crlf_to_json Function

Syntax

C result = crlf_to_json(txt as C [, delimiter as C ])

Arguments

txtCharacter

List of CR-LF delimited rows. The first row of text is assumed to be the field names.

delimiterCharacter

Default value is "|". The delimiter used to separate fields.

Description

Convert a CR-LF delimited list of records into a JSON object.

Discussion

Converts CR-LF delimited list of text to JSON. crlf_to_json can be used to convert the return value of a method such as sql_records_get to a JSON object. The first line in the CR-LF delimited list is expected to contain the field names for the records, separated by pipes "|".

If a different separator is used, such as commas, the second parameter can be used to indicate the delimiter if the delimiter between column values is not a pipe "|".

dim columns as c = "CustomerID|ContactName|Address|City|Country"
dim records as c = sql_records_get("::Name::northwind","SELECT FIRST 5 CustomerID, ContactName, Address, City, Country FROM CUSTOMERS","","")
records = columns + crlf() + records
dim json as c = crlf_to_json(records)

showvar(convert_utf8_to_acp(json))

The example above produces the following output:

[
	{
		"CustomerID": "ALFKI",
		"ContactName": "Maria Anders",
		"Address": "Obere Str. 57",
		"City": "Berlin",
		"Country": "Germany"
	},
	{
		"CustomerID": "ANATR",
		"ContactName": "Ana Trujillo",
		"Address": "Avda. de la Constitución 2222",
		"City": "México D.F.",
		"Country": "Mexico"
	},
	{
		"CustomerID": "ANTON",
		"ContactName": "Antonio Moreno",
		"Address": "Mataderos  2312",
		"City": "México D.F.",
		"Country": "Mexico"
	},
	{
		"CustomerID": "AROUT",
		"ContactName": "Thomas Hardy",
		"Address": "120 Hanover Sq.",
		"City": "London",
		"Country": "UK"
	},
	{
		"CustomerID": "BERGS",
		"ContactName": "Christina Berglund",
		"Address": "Berguvsvägen  8",
		"City": "Luleå",
		"Country": "Sweden"
	}
]

See Also